package com.jilujia.domain.question;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.question.repository.AnswerCommentRepository;
import com.jilujia.domain.question.repository.AnswerRepository;
import com.jilujia.framework.domain.AbstractPoEntityManager;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AnswerCommentManager extends AbstractPoEntityManager<Long, AnswerComment, AnswerCommentRepository> {

	@Autowired
	private AnswerRepository answerRepository;

	@Transactional
	public AnswerComment saveAnswerComment(AnswerComment answerCommentVo, Long userId) {

		// 持久化question
		Answer answer = answerRepository.findOne(answerCommentVo.getAnswer().getId());
		answerCommentVo.setAnswer(answer);

		// 初始化PO
		AnswerComment answerCommentPo = new AnswerComment();

		// new Answer comment
		Date now = new Date(System.currentTimeMillis());
		answerCommentPo.setCreatedDate(now);
		answerCommentPo.setModifyDate(now);
		answerCommentPo.setUserId(userId);

		// 直接发布

		// 把VO属性copy到PO上
		answerCommentPo.processVo(answerCommentVo);

		// 之前所有的动作都是为了生成完整的PO

		this.setInstance(answerCommentPo);
		this.save();

		return answerCommentPo;
	}
}
